<template>
  <div>
    <div class="tl-form-line-title sub-title">
      <span style="font-size:14px;color: #606266;">关联任务：</span>
      <div class="toolbar">
        <el-button type="text" size="medium" :disabled="disabled || milepost.planfinishtime === undefined || milepost.planfinishtime === null" icon="el-icon-circle-plus-outline" @click="openAddDialog('editForm')"><span>新增任务</span><span v-if="milepost.planfinishtime === undefined || milepost.planfinishtime === null">(须填写里程碑及任务计划完成日期)</span></el-button>
      </div>
    </div>
    <!--表格渲染-->
    <el-table class="tl-table" ref="mainTable" v-loading="loading" :row-style="tableRowStyle" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
      <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
      <el-table-column prop="taskname" label="任务名称" min-width="200px" sortable="custom" sort-by="taskname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="generatetaskcode" label="任务编号" width="100px" align="left" header-align="center" :show-overflow-tooltip="true">
      <template v-slot="scope">
        {{ parseInt(index + 1) + '-' + parseInt(scope.$index + 1) }}
      </template>
      </el-table-column>
      <el-table-column prop="responsible" label="责任人" width="85px" sortable="custom" sort-by="responsible" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="planstartdate" label="计划开始日期" width="120px" sortable="custom" sort-by="planstartdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="planenddate" label="计划完成日期" width="120px" sortable="custom" sort-by="planenddate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="taskdetail" label="任务内容" min-width="360px" sortable="custom" sort-by="taskdetail" align="left" header-align="center" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="post" label="关键决策客户部门职务" width="200px" sortable="custom" sort-by="post" align="center" header-align="center" :show-overflow-tooltip="true" v-if="customerTaskFlag"> </el-table-column>
      <el-table-column prop="dispcustomerapproval" label="任务支持程度" width="120px" sortable="custom" sort-by="customerapproval" align="center" header-align="center" :show-overflow-tooltip="true" v-if="customerTaskFlag"> </el-table-column>
      <el-table-column prop="customerattach" label="客户接触现状" width="300px" sortable="custom" sort-by="customerattach" align="center" header-align="center" :show-overflow-tooltip="true" v-if="customerTaskFlag"> </el-table-column>
      <el-table-column prop="dispneedsupport" label="是否需要系统部支持" width="200px" sortable="custom" sort-by="needsupport" align="center" header-align="center" :show-overflow-tooltip="true" v-if="customerTaskFlag"> </el-table-column>
      <el-table-column prop="improveplan" label="客户关系改善计划" width="300px" sortable="custom" sort-by="improveplan" align="center" header-align="center" :show-overflow-tooltip="true" v-if="customerTaskFlag"> </el-table-column>
      <el-table-column label=" " width="40px" v-slot="scope" fixed="right">
        <el-dropdown trigger="hover">
          <el-button type="text"><i class="iconfont icon-xiala"></i></el-button>
          <el-dropdown-menu>
            <el-dropdown-item type="text" icon="el-icon-edit" @click.native="openEditDialogForTask('editForm', scope.row.id)" v-if="!disabled">编辑</el-dropdown-item>
            <el-dropdown-item type="text" icon="el-icon-delete" @click.native="doDelete(scope.row.id)" v-if="!disabled && (showDeleteButton(scope.row) || provincePmChangeFlag)">删除</el-dropdown-item>
            <el-dropdown-item type="text" icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
            <el-dropdown-item type="text" icon="el-icon-edit" @click.native="openAddManagerDialog('managerEditForm', scope.row)" v-if="isPM">差距与改进</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <!-- <el-button @click="openViewDialog('editFeedbackList', scope.row.id)" :disabled="scope.row.taskstate === '0'">
            历史进度
          </el-button> -->
      </el-table-column>
    </el-table>
    <tl-edit ref="editForm" @refresh="doRefresh" :milepost="milepost" :milepostname="milepostname" :milepostcode="milepostcode" :planstate="planstate" :projectinfo="projectinfo" :customerTaskFlag="customerTaskFlag"></tl-edit>
    <tl-manager-edit ref="managerEditForm"></tl-manager-edit>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import { checkPermission } from '@tapui/utils/permission'
import { validateNull } from '@tapui/utils/validate'
import tlEdit from './editTask'
import tlProject from '../mpmProjectTask/editProjectRef'
import tlProjecttaskTemplate from '../mpmProjectTask/editProjectTaskTemplateRef'
import tlEditFeedbacklist from '../mpmProjectTaskFeedback/refList'
import tlManagerEdit from '../mpmProjectTaskFeedback/managerEdit'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
import { filterObjectNull, rmbFormat } from '@tapui/utils/util'
import { MPM_TASK_STATE } from '@/const/const'
import '../../../../static/images/iconfo/iconfont.css'
export default {
  name: 'mpmProjectTask',
  mixins: [mixin_list],
  components: { tlEdit, tlProject, tlProjecttaskTemplate, tlEditFeedbacklist, MPM_TASK_STATE, tlManagerEdit },
  data() {
    return {
      description: '项目任务-列表查询',
      urlPrefix: '/mpm/projectTask/',
      url: {
        queryCodeList: '/mpm/projectTask/dataCodeList'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'sortcode asc,planenddate asc,planstartdate asc,id ',
        sord: 'asc'
      },
      // 分页参数
      pageInfo: {
        currentPage: 1,
        pageSize: 2000
      },
      canDelete: true,
      isState: 0,
      canEdit: false,
      reduceHeight: 325
    }
  },
  props: {
    projectinfo: {},
    milepostcode: {
      type: [Number, String],
      default: '-1'
    },
    milepostname: {
      type: [Number, String],
      default: ''
    },
    planstate: {
      type: [Number, String],
      default: ''
    },
    index: {
      type: [Number, String],
      default: 0
    },
    disabled: {
      type: Boolean,
      default: false
    },
    milepost: {},
    customerTaskFlag: {
      type: Boolean,
      default: false
    },
    provincePmChangeFlag: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    // 里程碑及任务下任务列表的删除按钮显示/隐藏权限,任务为开始且不为DMD任务
    showDeleteButton() {
      return (row) => {
        if (row.taskstate === MPM_TASK_STATE.notStart) {
          return true
        }
        return false
      }
    },
    isPM() {
      if (this.$store.getters.projectroles) {
        return this.$store.getters.projectroles.indexOf('pm') > -1
      } else {
        return false
      }
    }
  },
  watch: {
    milepost: function(oldV, newV) {
      this.doQuery()
    }
  },
  methods: {
    // 重写刷新方法，当更新了项目状态时回调
    doRefresh(dateChanged) {
      this.loadData({})
      if (dateChanged) {
        this.$emit('refresh')
      }
    },
    beforeOpenAddDialog(editForm, rowData) {
      this.$parent.$parent.$parent.$parent.saveMilepost(this.index)
      return true
    },
    beforeLoadData() {
      this.queryParam.qry_milepostcode = this.milepostcode
      return true
    },
    /** 配置编辑权限 */
    doRowEdit(row, column, event) {
      let canEdit = false
      // 当任务的状态为未开始或进行中时，支持策划变更时修改任务的状态
      if ([MPM_TASK_STATE.notStart, MPM_TASK_STATE.inComplete].includes(row.taskstate)) {
        canEdit = true
      }
      if (!this.disabled && canEdit) {
        return true
      } else {
        return false
      }
    },
    /** 获取查询参数 */
    getQueryParams() {
      let param = {}
      if (this.superQueryParams) {
        param['superQueryParams'] = encodeURI(this.superQueryParams)
      }
      Object.assign(param, this.queryParam, this.sorterInfo, this.filtersInfo)
      param.currentPage = this.pageInfo.currentPage
      param.pageSize = this.pageInfo.pageSize
      param.qry_projectcode = this.projectinfo.projectcode
      this.queryParam.qry_milepostcode = ''
      // 标识为策划书中的任务查询
      param.taskForPlan = '1'
      return filterObjectNull(param)
    },
    /** 格式化任务进度 */
    percentageFormat(row, column, cellValue, index) {
      let taskScheduleValue = row[column.property]
      if (typeof taskScheduleValue === 'undefined' || taskScheduleValue === null || taskScheduleValue === '') {
        return '0%'
      }
      taskScheduleValue = taskScheduleValue + '%'
      return taskScheduleValue
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      // 双击都进入编辑页面，因为排序码需要支持随时更新
      this.openEditDialogForTask('editForm', row[this.$refs.mainTable.rowKey])
    },
    /** 打开编辑对话框 */
    openEditDialogForTask(editForm, id) {
      // 判断里程碑的计划达成日期是否填写，如果没有填写则提示需要先填
      if (validateNull(this.milepost.planfinishtime)) {
        this.$tap.alert('请填写里程碑计划达成日期！')
        return
      }
      this.$refs[editForm].id = id
      this.$refs[editForm].isAdd = false
      this.$refs[editForm].visible = true
      if (this.disabled) {
        this.$refs[editForm].disableEditSubmit = true
      } else {
        this.$refs[editForm].disableEditSubmit = false
      }
    },
    /** 打开差距与改进新增对话框 */
    openAddManagerDialog(editForm, rowData) {
      this.$refs[editForm].isAdd = true
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
      this.$refs[editForm].taskInfo = rowData
    },
    // 行样式，价格高于红线价格标红
    tableRowStyle(row, index) {
      if (row.row.taskcopyedit && row.row.taskcopyedit === '0') {
        return { 'background-color': '#ff8c2e', color: '#000' }
      }
    }
  }
}
</script>

<style scoped></style>
